import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:seedfour/app/views/module_main/main_mine/state.dart';
import 'package:seedfour/toolkit/typedef/function.dart';

import '../../../../widget/tag.dart';

class MinePersonal extends StatelessWidget {
  /// 数据源
  final MineState data;

  /// 点击切换控制台
  final ParamVoidCallback onChangeRole;

  /// 点击设置
  final ParamVoidCallback onClickSetting;

  const MinePersonal({
    Key? key,
    required this.data,
    required this.onChangeRole,
    required this.onClickSetting,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('assets/images/mine_background.png'),
          alignment: Alignment.topCenter,
          fit: BoxFit.fill,
        ),
      ),
      child: Column(children: [
        AppBar(
          elevation: 0,
          centerTitle: false,
          backgroundColor: Colors.transparent,
          systemOverlayStyle: const SystemUiOverlayStyle(
            statusBarColor: Colors.transparent, // 确保状态栏背景透明
            statusBarIconBrightness: Brightness.dark, // 将状态栏图标设置为黑色
          ),
          title: Text(
            '我的',
            style: TextStyle(
              fontSize: 22.sp,
              color: AppColor.colorFontBlack,
            ),
          ),
          actions: [
            IconButton(
              icon: SvgPicture.asset(
                'assets/icons/ic_setting.svg',
                width: kIconSizeMini,
              ),
              onPressed: () => onClickSetting(),
            ),
          ],
        ),
        SizedBox(height: kSpaceSmall),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: kSpaceMedium),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              _buildAvatar(),
              SizedBox(width: kSpaceMini),
              _buildTitle(),
              SizedBox(width: kSpaceMini),
              _buildButton(),
            ],
          ),
        ),
      ]),
    );
  }

  Widget _buildAvatar() {
    return CircleAvatar(
      radius: kIconSizeMini,
      backgroundImage: NetworkImage(data.personalInfo.value?.avatar ?? ''),
    );
  }

  Widget _buildTitle() {
    return Obx(
      () => Expanded(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  data.personalInfo.value?.name ?? '',
                  style: TextStyle(
                    fontSize: kFontSizeLarge,
                    fontWeight: AppFont.fontMedium,
                    color: AppColor.colorFontBlack,
                  ),
                ),
                SizedBox(width: kSpaceMini),
                Tag(
                  text: data.role.value == 'me' ? '普通用户' : '机构管理',
                  textColor: data.role.value == 'me'
                      ? AppColor.colorFontGreen
                      : AppColor.colorFontOrange,
                  textSize: kFontSizeTiny,
                  backgroundColor: data.role.value == 'me'
                      ? AppColor.colorTagGreen
                      : AppColor.colorTagOrange,
                  padding: EdgeInsets.symmetric(
                    horizontal: 6.w,
                    vertical: 3.w,
                  ),
                ),
              ],
            ),
            SizedBox(height: kSpaceTiny),
            Text(
              data.personalInfo.value?.currentCompany.name ?? '',
              style: TextStyle(fontSize: kFontSizeMini, color: Colors.grey),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildButton() {
    return TextButton(
        onPressed: () => onChangeRole(),
        style: TextButton.styleFrom(
          backgroundColor: AppColor.colorTagBackground,
          padding: EdgeInsets.symmetric(
              horizontal: kSpaceMini, vertical: kSpaceTiny),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(kRadiusSmall),
          ),
        ),
        child: Row(
          children: [
            Text(
              '切换账户',
              style: TextStyle(
                fontSize: kFontSizeMini,
                color: AppColor.colorFontGrey,
              ),
            ),
            Icon(
              Icons.sync_alt,
              size: 14.w,
              color: AppColor.colorFontGrey,
            )
          ],
        ));
  }
}
